package com.lw.carmanage.service.impl;

import com.lw.carmanage.enums.CheckType;
import com.lw.carmanage.events.MyUserRoleEvent;
import com.lw.carmanage.exceptions.AddBaseException;
import com.lw.carmanage.exceptions.MyErrorCode;
import com.lw.carmanage.mapper.SysUserMapper;
import com.lw.carmanage.pojo.SysUser;
import com.lw.carmanage.service.SysUserService;
import com.lw.carmanage.utils.MyStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/30 10:56
 */
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public void setbCryptPasswordEncoder(BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    private SysUserMapper userMapper;

    @Autowired
    public void setUserMapper(SysUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void addUser(SysUser user) {


        //判断是不是为空
        Assert.isTrue(!user.isEmpty(CheckType.ADD), () -> {
            throw new AddBaseException("数据不能为空", MyErrorCode.DATA_NULL);
        });

        //校验手机号和邮箱格式
        //校验手机号
        Assert.isTrue(MyStringUtil.check(user.getMobile(), MyStringUtil.mobileRegex), () -> {
            throw new AddBaseException("手机号不合法", MyErrorCode.PHONE_REG_NOT_MACTH);
        });
        //校验邮箱
        Assert.isTrue(MyStringUtil.check(user.getEmail(), MyStringUtil.emailRegex), () -> {
            throw new AddBaseException("邮箱格式不正确", MyErrorCode.MAIL_REG_NOT_MACTH);
        });
        //校验唯一数据
        SysUser sysUser = userMapper.findByUserName(user.getUsername());
        Assert.isNull(sysUser, () -> {
            throw new AddBaseException("用户名已存在", MyErrorCode.USER_ALREADY_EXIST);
        });

        sysUser = userMapper.findByEmail(user.getEmail());
        Assert.isNull(sysUser, () -> {
            throw new AddBaseException("邮箱已存在", MyErrorCode.EMAIL_ALREADY_EXIST);
        });

        sysUser = userMapper.findByMobile(user.getMobile());
        Assert.isNull(sysUser, () -> {
            throw new AddBaseException("手机号已存在", MyErrorCode.PHONE_ALREADY_EXIST);
        });

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode(user.getPassword());//对于传入的密码进行加密
        user.setPassword(password);
        user.setCreateTime(new Date());
        //保存到数据库
        userMapper.addUser(user);

        logger.error("添加成功");

        //给新增用户添加权限，为什么类型的人员，更新表
        context.publishEvent(new MyUserRoleEvent(user.getUsername()));
    }

}
